package hiring.tong.com.company.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import hiring.tong.com.company.dao.CompanyDao;
import hiring.tong.com.company.entity.CompanyEntity;


/**
 * Servlet implementation class CompanyServlet
 */
public class CompanyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CompanyDao companyDao=new CompanyDao();
    

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String command=request.getParameter("command");
		if("get".equals(command)){
			get(request,response);
		}
		else if("main".equals(command)){
			main(request,response);
		}
		else if("exit".equals(command)){
			exit(request,response);
		}
		else if("register".equals(command)){
			register(request,response);
		}
		else if("fail".equals(command)){
			fail(request,response);
		}
		else if("preUpdate".equals(command)){
			preUpdate(request,response);
		}
		else if("update".equals(command)){
			update(request,response);
		}
		else{
			throw new RuntimeException("非法操作");
		}
	}

	private void fail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		String code=request.getParameter("code");
		
		
		request.setAttribute("error","用户账户和密码不正确，请重新登录");
		request.setAttribute("code", code);
		request.getRequestDispatcher("view/company/company-login.jsp").forward(request,response);
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		// TODO Auto-generated method stub
		CompanyEntity companyEntity=(CompanyEntity)request.getSession().getAttribute("company");
		int id=companyEntity.getId();
		String code=request.getParameter("code");
		String password=request.getParameter("pwd");
		String name=request.getParameter("username");
		String email=request.getParameter("email");
		String phone=request.getParameter("phone");
		String linkman=request.getParameter("linkman");
		String address=request.getParameter("address");
		String slogan=request.getParameter("slogan");
		int numbers=Integer.parseInt(request.getParameter("number"));
		CompanyEntity company=new CompanyEntity();
		company.setAddress(address);
		company.setCode(code);
		company.setEmail(email);
		company.setLinkman(linkman);
		company.setName(name);
		company.setNumbers(numbers);
		company.setPassword(password);
		company.setSlogan(slogan);
		company.setPhone(phone);
		company.setId(id);
		int row=companyDao.update(company);
		if(row==1){
			
			response.sendRedirect("CompanyServlet?command=exit");
		}
		else{
			request.setAttribute("mession","修改失败，请重新修改");
			request.getRequestDispatcher("view/company/company-update.jsp").forward(request, response);
		}
	}

	private void preUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		CompanyEntity companyEntity=(CompanyEntity)request.getSession().getAttribute("company");
		int id=companyEntity.getId();
		CompanyEntity company=companyDao.preUpdate(id);
		request.setAttribute("company", company);
		request.getRequestDispatcher("view/company/company-update.jsp").forward(request, response);
	}

	private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String code=request.getParameter("code");
		String password=request.getParameter("pwd");
		String name=request.getParameter("username");
		String email=request.getParameter("email");
		String phone=request.getParameter("phone");
		String linkman=request.getParameter("linkman");
		String address=request.getParameter("address");
		String slogan=request.getParameter("slogan");
		int numbers=Integer.parseInt(request.getParameter("number"));
		CompanyEntity company=new CompanyEntity();
		company.setAddress(address);
		company.setCode(code);
		company.setEmail(email);
		company.setLinkman(linkman);
		company.setName(name);
		company.setNumbers(numbers);
		company.setPassword(password);
		company.setSlogan(slogan);
		company.setPhone(phone);
		
		int row=companyDao.register(company);
		if(row==1){
			request.getRequestDispatcher("view/company/company-login.jsp").forward(request, response);	
		}
		else{
			request.setAttribute("mession","注册失败，请重新注册");
			request.getRequestDispatcher("view/company/company-register.jsp").forward(request, response);
		}
		
		
		
		
	}

	private void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.getSession().invalidate();
		request.getRequestDispatcher("view/company/company-login.jsp").forward(request,response);
	}

	private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.getRequestDispatcher("view/company/company-main.jsp").forward(request, response);
	}

	private void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String code=request.getParameter("code");
		String password=request.getParameter("password");
		CompanyEntity company=companyDao.get(code,password);
		if("true".equals(request.getParameter("remberinfo"))){
			Cookie cookie=new Cookie("codes",code);
			Cookie cookies=new Cookie("passwords",password);
			Cookie cookiess=new Cookie("remberinfos",request.getParameter("remberinfo"));
			cookie.setMaxAge(60 * 30);
			cookies.setMaxAge(60 * 30);
			cookiess.setMaxAge(60 * 30);
			response.addCookie(cookie);
			response.addCookie(cookies);
			response.addCookie(cookiess);
		}
		
		if(company==null){
//			request.setAttribute("code", code);
//			request.setAttribute("error", "用户账户和密码不正确，请重新登录");
//			request.getRequestDispatcher("view/company/company-login.jsp").forward(request,response);
			 response.sendRedirect("CompanyServlet?command=fail&code="+code);
		}
		else{
			if("true".equals(request.getParameter("remberinfos"))==false){
				Cookie cookie=new Cookie("codes",code);
				Cookie cookies=new Cookie("passwords",password);
				
				cookie.setMaxAge(60 * 30);
				cookies.setMaxAge(60 * 30);
			
				response.addCookie(cookie);
				response.addCookie(cookies);
			
			}
			request.getSession().setAttribute("company",company);
			
			response.sendRedirect("CompanyServlet?command=main");
		}
	}
		
	}


